package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.pill;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalPillIndexBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillTotalBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalPillIndexPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPillIndexViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class InHospitalPillIndexActivity extends BaseActivity<ActivityInHospitalPillIndexBinding,InHospitalPillIndexViewModel,InHospitalPillIndexPresenter> {
    //PDA扫码广播
    private ScanReceiverHospitalTake mScanReceiver;
    private IntentFilter intentFilter;

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        binding.hospitalIndexTitle.titleTextTv.setText("包药机扫码");
        binding.hospitalIndexTitle.titleBackTv.setText("");
        binding.hospitalIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.hospitalIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //显示操作人姓名
        if(null != PreferenceManager.getInstance().getLUserName() || "".equals(PreferenceManager.getInstance().getLUserName())){
            binding.tvPharmacyCname.setText("工号: "+ PreferenceManager.getInstance().getLUserNum());
        }
        mScanReceiver = new ScanReceiverHospitalTake();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);
        binding.rvDoorPutIndex.setLayoutManager(new LinearLayoutManager(this));
        binding.rvDoorPutIndex.setPullRefreshEnabled(false);

        RxBus.getInstance().tObservable(Contants.INHOSPITAL_PILL_INDEX_PENDING_COUNT, List.class).subscribe(new Consumer<List<InHospitalPillTotalBeen>>() {
            @Override
            public void accept(List<InHospitalPillTotalBeen> putTotalBeens) throws Exception {
                if(null == putTotalBeens.get(0).getDJJLID()|| putTotalBeens.get(0).getDJJLID().equals("") ){
                    binding.tvHospitalIndexPendingNumber.setText("0");
                    return;
                }
                binding.tvHospitalIndexPendingNumber.setText("" + putTotalBeens.size());
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_pill_index;
    }

    @Override
    protected Class<InHospitalPillIndexPresenter> getPresenterClass() {
        return InHospitalPillIndexPresenter.class;
    }

    @Override
    protected Class<InHospitalPillIndexViewModel> getViewModelClass() {
        return InHospitalPillIndexViewModel.class;
    }
    @Override
    protected void onResume() {
        super.onResume();
        presenter.getDataList();
        registerReceiver(mScanReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mScanReceiver);
    }


    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiverHospitalTake extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case Contants.WOW_NEW_ADD:
                    String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                    presenter.loadData(stringExtra);
                    break;
            }
        }
    }
}
